<template>
  <div>
    <div class="box">
      <el-button type="primary" @click="handleAdd">添加分组</el-button>
    </div>
    <div class="box">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column
          prop="groupId"
          label="ID"
          width="80"
        ></el-table-column>
        <el-table-column
          prop="groupName"
          label="分组名称"
        ></el-table-column>
        <el-table-column prop="createTime" label="发布时间" width="180"></el-table-column>
        <el-table-column prop="updateTime" label="修改时间" width="180"></el-table-column>
        <el-table-column label="操作" width="280">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="warning"
              @click="handleEdit(scope.$index, scope.row)"
            >
              编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 添加/修改 -->
    <el-dialog title="作者分组" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="分组名称" :label-width="formLabelWidth" prop="groupName">
          <el-input v-model="form.groupName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {addAuthorGroup,setAuthorGroup,delAuthorGroupById,getAuthorGroupById,getAuthorGroupList} from '@/api/news.js'
  export default {
    name: 'AuthorGroup',
    data() {
      return {
        tableData: [],
        dialogFormVisible: false,
        formLabelWidth: '120px',
        form: {
          groupId: 0,
          groupName: ''
        },
        rules: {
          groupName: [{ required: true, message: '请输入分组名称', trigger: 'blur' }],
        },
      }
    },
    mounted() {
      this.loadList()
    },
    methods: {
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      loadList() {
        getAuthorGroupList({}).then(res => {
          if(res.code == 200) {
            this.tableData = res.data
          }
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.form.groupId>0) {
              setAuthorGroup(this.form).then(res => {
                if(res.code == 200) {
                  this.$message.success("修改成功")
                  this.dialogFormVisible = false
                  this.loadList()
                }
                else {
                  this.$message.error("修改失败")
                }
              })
            }
            else {
              addAuthorGroup(this.form).then(res => {
                if(res.code == 200) {
                  this.$message.success("添加成功")
                  this.dialogFormVisible = false
                  this.loadList()
                }
                else {
                  this.$message.error("添加失败")
                }
              })
            }
          } else {
            return false;
          }
        });
      },
      handleAdd() {
        this.dialogFormVisible = true
      },
      handleEdit(index, row) {
        let param = {id: row.groupId}
        getAuthorGroupById(param).then(res => {
          if(res.code == 200) {
            this.form = res.data
            this.dialogFormVisible = true
          }
        })
      },
      handleDelete(index, row) {
        let param = {id: row.groupId}
        delAuthorGroupById(param).then(res => {
          if(res.code == 200) {
            this.$message.success("删除成功")
          }
          else {
            this.$message.error("删除失败")
          }
        })
      },
    }
  }
</script>

<style scoped>
.box {
  margin: 10px;
}
</style>
